$(function(){
    var bannerlis = document.querySelector(".img").children;
    var dots = document.querySelector(".dots").children;
    //自动播放
    //定时器
    var timer = null;
    var num = 0;
    //封装修改类名函数
    function play(key){
        //改类名
        for (var i = 0; i < bannerlis.length; i++) {
            bannerlis[i].className = "";
            dots[i].className = "";
        }
        dots[key].className = "cur";
        bannerlis[key].className = "cur";
    }

    //封装自动播放函数
    function autoplay(){
        timer = setInterval(function(){
            num++;			
            if(num>bannerlis.length-1){
                num=0;
            }
            play(num);						
        },3000);
    }
        
    var banner = document.querySelector(".img");
    //鼠标移上暂停
    banner.onmouseenter = function(){
        clearInterval(timer);
    }
    //鼠标离开继续
    banner.onmouseleave = function(){
        autoplay();
    }

    //小圆点点击切换
    for (var i = 0; i < dots.length; i++) {
        dots[i].index = i;
        dots[i].onclick = function(){
            play(this.index);
            num = this.index;
        }
    }

    //左右按钮点击切换
    var prev = document.querySelector(".btnLeft");
    var next = document.querySelector(".btnRight");
    prev.onclick = function(){
        num--;
        if(num<0){
            num = bannerlis.length - 1;
        }
        play(num);
    }
    next.onclick = function(){
        num++;
        if(num> bannerlis.length - 1){
            num = 0;
        }
        play(num);
    }
})